// pages/home/pages/forget/forget.js
var r = require('../../../../utils/request.js')
let timer
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    code: '',
    start: false,
    count: 60,

    password1: '',
    password2: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  // 获取值
  getval(e) {
    let type = e.currentTarget.dataset.type

    if (type == 1) {
      this.setData({
        phone: e.detail.value
      })
    } else if (type == 2) {
      this.setData({
        code: e.detail.value
      })
    } else if (type == 3) {
      this.setData({
        password1: e.detail.value
      })
    } else if (type == 4) {
      this.setData({
        password2: e.detail.value
      })
    }
    console.log(e.detail.value)
  },


  // 获取验证码
  getCode() {
    var param = {
      tel: this.data.phone
    }
    r.toPost('/login/senCheckMsg', param, (res) => {
      console.log(res.retData)
      wx.showToast({
        icon: 'none',
        title: res.retMsg
      });
      this.setData({
        start: true
      })
      var num = this.data.count
      timer = setInterval(() => {
        num--;
        this.setData({
          count: num
        });
        if (this.data.count <= 0) {
          clearInterval(timer);
          this.setData({
            start: false
          });
          this.setData({
            count: 60
          })
        }
      }, 1000);
    })
  },

  submit(){
    let myreg = /^1[3456789]\d{9}$/;
    if (!myreg.test(this.data.phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确手机号码！'
      });
      return
    }
    if(!this.data.code){
      wx.showToast({
        icon: 'none',
        title: '验证码不正确！'
      });
      return
    }
    if(!this.data.password1){
      wx.showToast({
        icon: 'none',
        title: '请输入密码'
      });
      return
    }
    if(this.data.password1 != this.data.password2){
      wx.showToast({
        icon: 'none',
        title: '密码不一致'
      });
      return
    }
    let param = {
      phone:this.data.phone,
      msgCode:this.data.code,
      newPwd:this.data.password2
    }
    r.toPost('/login/forgetPwd', param, (res) => {
      wx.showToast({
        title: '操作成功！',
        icon:'none'
      })
      wx.clearStorage()
      setTimeout(() => {
        wx.reLaunch({
          url: '../login/login',
        })
      }, 1000);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})